package com.two.zelin.controller;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DataBaseUtil {	
		//声明获取三个变量
		public static final String url  = "jdbc:mysql://106.52.241.132:3306/dingdan?useSSL=false&serverTimezone=UTC";
		public static final String user ="root";
		public static final String password ="two123456";
			
			
		static {
			//5.x的驱动加载方式，驱动名称为：com.mysql.jdbc.Driver
			try {
				DriverManager.registerDriver(new com.mysql.jdbc.Driver());
			} catch (SQLException e) {
					
				e.printStackTrace();
			}	
		}  	
/**    
 * 获取数据库的连接
 * @return 返回数据库连接对象
 * 
 * 
 * */
		public static  Connection getConnection() {
			Connection conn = null;
			try {
				conn = DriverManager.getConnection(url,user,password);
			}catch (SQLException e) {
				e.printStackTrace();
			}  
			return conn;		
		}

/**
	* 关闭数据库相关数据的方法
	*@param conn  连接对象
	*@param  PS   声明对象
	*@param  rs   结果对象
	*   */		
			
		public static void closeResource(Connection conn,PreparedStatement ps,ResultSet rs ) {
			try {
				if(rs != null && !rs.isClosed()) {
						rs.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				if(ps !=null &&!ps.isClosed()) {
						ps.close();		
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				if(conn !=null && !conn.isClosed()) {
					
						conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			
		
		}	
}